/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myconfession.dao;

// com.sun.org.apache.xerces.internal.util.XML11Char;
import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;
import javax.xml.datatype.DatatypeConfigurationException;
import models.PostBO;
import myconfession.dto.post.Post;
import myconfession.dto.post.PostList;
import myconfession.utils.SQLConnection;
import myconfession.utils.XMLUtility;

/**
 *
 * @author BaoLA
 */
public class PostDAO {

    public static PostList getPostList() {
        Connection con = null;
        PreparedStatement stm = null;
        ResultSet rs = null;
        PostList list = null;
        try {
            con = SQLConnection.getConnection();
            stm = con.prepareStatement("exec sp_getAllPost");
            rs = stm.executeQuery();
            list = new PostList();
            while (rs.next()) {
                int postId = rs.getInt("id");
                int pageId = rs.getInt("pageId");
                String postContent = rs.getString("postContent");
                Timestamp dateTime = rs.getTimestamp("date");
                String status = rs.getString("status");
                String fbPostId = rs.getString("fbPostId");
                Post p = new Post();
                p.setId(new BigInteger(postId + ""));
                p.setPageId(new BigInteger(pageId + ""));
                p.setPostContent(postContent);
                p.setDate(XMLUtility.getXMLGregorianCalendar(dateTime));
                p.setStatus(status);
                p.setFbPostId(fbPostId);

                list.getPost().add(p);
            }
        } catch (DatatypeConfigurationException ex) {
            Logger.getLogger(PostDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NamingException ex) {
            Logger.getLogger(PageDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(PageDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (stm != null) {
                    stm.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (SQLException ex) {
                Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return list;

    }

    public static int sendPost(Post p) {
        Connection con = null;
        CallableStatement stm = null;
        try {
            con = SQLConnection.getConnection();
            stm = con.prepareCall("{call sp_addPost(?,?,?,?,?)}");
            stm.registerOutParameter(1, Types.INTEGER);
            stm.setInt(2, p.getPageId().intValue());
            stm.setString(3, p.getPostContent());
            stm.setTimestamp(4, XMLUtility.getSqlTimeStamp(p.getDate()));
            stm.setString(5, p.getStatus());
            int row = stm.executeUpdate();
            if (row > 0) {
                return stm.getInt(1);
            } else {
                return 0;
            }

        } catch (NamingException ex) {
            Logger.getLogger(PostDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(PostDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                if (stm != null) {
                    stm.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (SQLException ex) {
                Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return 0;
    }

    public static boolean updatePost(int id, String content, String status, String fbPostId) {
        Connection con = null;
        PreparedStatement stm = null;
        try {
            con = SQLConnection.getConnection();
            stm = con.prepareStatement("exec sp_updatePost ?,?,?,?");
            stm.setInt(1, id);
            stm.setString(2, content);
            stm.setString(3, status);
            stm.setString(4, fbPostId);
            int rs = stm.executeUpdate();
            if (rs > 0) {
                return true;
            }
        } catch (NamingException ex) {
            Logger.getLogger(PageDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(PageDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                if (stm != null) {
                    stm.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (SQLException ex) {
                Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return false;
    }

    public static boolean rejectPost(int id) {
        Connection con = null;
        PreparedStatement stm = null;
        try {
            con = SQLConnection.getConnection();
            stm = con.prepareStatement("update Posts set status= ? where id = ?");
            stm.setString(1, PostBO.READ);
            stm.setInt(2, id);
            int rs = stm.executeUpdate();
            if (rs > 0) {
                return true;
            }
        } catch (NamingException ex) {
            Logger.getLogger(PostDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(PostDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                if (stm != null) {
                    stm.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (SQLException ex) {
                Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return false;
    }
}
